---------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  wk04 FGLS Answers.log
  log type:  text
 opened on:  15 Feb 2026, 06:28:19

. 
. clear all

. 
. import excel "Newton Ma properties FY2025.xlsx", sheet("Sheet1") firstrow

. 
. keep Saleprice1 Saledate1 UseCodeDescription Lotsize Rooms Bedrooms Baths HBaths Livingarea

. 
. *tab UseCodeDescription, sort missing
. 
. keep if UseCodeDescription=="SINGLE FAMILY"
(11,642 observations deleted)

. 
. gen prior_to_2024=(Saledate1 < mdy(1,1,2024) & Saledate1 != .)

. replace prior_to_2024 = . if Saledate1 == .
(243 real changes made, 243 to missing)

. 
. tab prior_to_2024, missing

prior_to_20 |
         24 |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |      1,305        7.70        7.70
          1 |     15,390       90.86       98.57
          . |        243        1.43      100.00
------------+-----------------------------------
      Total |     16,938      100.00

. 
. keep if Saledate1 >= mdy(1,1,2024) & Saledate1 != .
(15,633 observations deleted)

. 
. sort Saleprice1

. 
. tab Saleprice1 if Saleprice1 <=300000

Sale price1 |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |        419       63.01       63.01
          5 |          1        0.15       63.16
         10 |         71       10.68       73.83
         99 |          1        0.15       73.98
        100 |        169       25.41       99.40
        110 |          1        0.15       99.55
      99768 |          1        0.15       99.70
     300000 |          2        0.30      100.00
------------+-----------------------------------
      Total |        665      100.00

. 
. keep if Saleprice1 > 300000 & Saleprice1 != .
(665 observations deleted)

. 
. sum Lotsize, detail

                          Lot size
-------------------------------------------------------------
      Percentiles      Smallest
 1%         2700           1200
 5%       4830.5           1200
10%       5680.5           1565       Obs                 640
25%         7655           1640       Sum of Wgt.         640

50%      10261.5                      Mean           12499.27
                        Largest       Std. Dev.      9547.283
75%        14403          59147
90%        20266          60359       Variance       9.12e+07
95%        27653          70380       Skewness       5.259118
99%        48591         138128       Kurtosis       54.30105

. 
. 
. drop if Lotsize < 2700
(6 observations deleted)

. drop if Lotsize > 48591
(6 observations deleted)

. 
. summ Saleprice1 Saledate1 Lotsize Livingarea Rooms Bedrooms Baths HBaths 

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
  Saleprice1 |        628     2097634     1265350     336807   1.00e+07
   Saledate1 |        628    23604.03    114.7724      23377      23831
     Lotsize |        628     12041.2    7034.803       2700      48591
  Livingarea |        628    2841.054     1358.51        554       7939
       Rooms |        628    8.531847    2.167751          4         19
-------------+---------------------------------------------------------
    Bedrooms |        628    3.992038    1.160872          2          9
       Baths |        628     3.06051    1.687604          1          9
      HBaths |        555    .9621622    .5890171          0          5

. 
. gen price = Saleprice1

. gen sqft = Livingarea

. gen baths = Baths

. gen hbaths = HBaths
(73 missing values generated)

. gen lotsize = Lotsize

. 
. eststo clear

. 
. reg price lotsize sqft baths hbaths

      Source |       SS           df       MS      Number of obs   =       555
-------------+----------------------------------   F(4, 550)       =    390.06
       Model |  6.9501e+14         4  1.7375e+14   Prob > F        =    0.0000
    Residual |  2.4500e+14       550  4.4545e+11   R-squared       =    0.7394
-------------+----------------------------------   Adj R-squared   =    0.7375
       Total |  9.4001e+14       554  1.6968e+12   Root MSE        =    6.7e+05

------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     lotsize |   46.02374   5.321376     8.65   0.000     35.57104    56.47645
        sqft |   356.4432   49.05774     7.27   0.000     260.0798    452.8067
       baths |   228265.4   31196.13     7.32   0.000     166987.3    289543.6
      hbaths |   180950.8   55417.35     3.27   0.001     72095.26    289806.4
       _cons |  -318356.1   71998.06    -4.42   0.000    -459780.9   -176931.3
------------------------------------------------------------------------------

. hettest

Breusch-Pagan / Cook-Weisberg test for heteroskedasticity 
         Ho: Constant variance
         Variables: fitted values of price

         chi2(1)      =   552.03
         Prob > chi2  =   0.0000

. eststo
(est1 stored)

. predict yhat
(option xb assumed; fitted values)
(73 missing values generated)

. predict uhat, res
(73 missing values generated)

. predict my_leverage, hat
(73 missing values generated)

. 
. gen uhat2 = uhat^2
(73 missing values generated)

. sum uhat2, detail

                            uhat2
-------------------------------------------------------------
      Percentiles      Smallest
 1%      4801019       790920.7
 5%     6.84e+08        1265925
10%     2.45e+09        1997771       Obs                 555
25%     1.56e+10        2620570       Sum of Wgt.         555

50%     7.28e+10                      Mean           4.41e+11
                        Largest       Std. Dev.      1.46e+12
75%     2.74e+11       9.24e+12
90%     9.16e+11       1.13e+13       Variance       2.14e+24
95%     1.54e+12       1.36e+13       Skewness       8.508497
99%     7.46e+12       2.12e+13       Kurtosis       96.65438

. 
. gen wgt_uhat2 = 1/uhat2
(73 missing values generated)

. sum wgt_uhat2

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
   wgt_uhat2 |        555    6.94e-09    7.01e-08   4.72e-14   1.26e-06

. replace wgt_uhat2 = wgt_uhat2/r(sum)
(555 real changes made)

. sum wgt_uhat2, detail

                          wgt_uhat2
-------------------------------------------------------------
      Percentiles      Smallest
 1%     3.48e-08       1.22e-08
 5%     1.69e-07       1.91e-08
10%     2.83e-07       2.30e-08       Obs                 555
25%     9.47e-07       2.81e-08       Sum of Wgt.         555

50%     3.57e-06                      Mean           .0018018
                        Largest       Std. Dev.      .0181807
75%     .0000167        .099008
90%     .0001061       .1298735       Variance       .0003305
95%     .0003792       .2049549       Skewness       13.98586
99%     .0540422       .3280449       Kurtosis       221.9735

. 
. list if wgt_uhat2 > .1 & wgt_uhat2 != .

     +-----------------------------------------------------------------------------------------------------+
175. | Saledate1 | Salepr~1 | UseCodeDesc~n | Lotsize  | Rooms  | Bedrooms  | Baths  | HBaths  | Living~a  |
     | 15jul2024 |  1300000 | SINGLE FAMILY |    6720  |     8  |        4  |     2  |      1  |     1881  |
     |-----------------------------------------------------------------------------------------+-----------|
     | pri~2024 |   price | sqft | baths | hbaths | lotsize | _est_e~1 |      yhat |      uhat | my_leve~e |
     |        0 | 1300000 | 1881 |     2 |      1 |    6720 |        1 | 1298874.9 | 1125.1334 |  .0034565 |
     |-----------------------------------------------------------------------------------------------------|
     |                        uhat2                     |                    wgt_uhat2                     |
     |                    1265925.2                     |                    .20495485                     |
     +-----------------------------------------------------------------------------------------------------+

     +-----------------------------------------------------------------------------------------------------+
370. | Saledate1 | Salepr~1 | UseCodeDesc~n | Lotsize  | Rooms  | Bedrooms  | Baths  | HBaths  | Living~a  |
     | 23feb2024 |  2000000 | SINGLE FAMILY |    9842  |     9  |        4  |     3  |      1  |     2802  |
     |-----------------------------------------------------------------------------------------+-----------|
     | pri~2024 |   price | sqft | baths | hbaths | lotsize | _est_e~1 |      yhat |      uhat | my_leve~e |
     |        0 | 2000000 | 2802 |     3 |      1 |    9842 |        1 | 1999110.7 |  889.3372 | .00210085 |
     |-----------------------------------------------------------------------------------------------------|
     |                        uhat2                     |                    wgt_uhat2                     |
     |                    790920.66                     |                    .32804492                     |
     +-----------------------------------------------------------------------------------------------------+

     +-----------------------------------------------------------------------------------------------------+
513. | Saledate1 | Salepr~1 | UseCodeDesc~n | Lotsize  | Rooms  | Bedrooms  | Baths  | HBaths  | Living~a  |
     | 16apr2024 |  2870000 | SINGLE FAMILY |   22662  |    10  |        4  |     3  |      1  |     3586  |
     |-----------------------------------------------------------------------------------------+-----------|
     | pri~2024 |   price | sqft | baths | hbaths | lotsize | _est_e~1 |      yhat |      uhat | my_leve~e |
     |        0 | 2870000 | 3586 |     3 |      1 |   22662 |        1 | 2868586.6 | 1413.4252 | .00662101 |
     |-----------------------------------------------------------------------------------------------------|
     |                        uhat2                     |                    wgt_uhat2                     |
     |                    1997770.7                     |                    .12987352                     |
     +-----------------------------------------------------------------------------------------------------+

. 
. sort wgt_uhat2

. gen newvar0 = sum(wgt_uhat2)

. gen nobs0 = _n

. scatter newvar0 nobs0 if uhat2 != .

. 
. graph export "ex04 A wgt_uhat2.png", replace
(file ex04 A wgt_uhat2.png written in PNG format)

. 
. 
. * HC-0 Heteroskedasticity Consistent 0
. reg price lotsize sqft baths hbaths [w = wgt_uhat2]
(analytic weights assumed)
(sum of wgt is 1)

      Source |       SS           df       MS      Number of obs   =       555
-------------+----------------------------------   F(4, 550)       >  99999.00
       Model |  1.4833e+14         4  3.7082e+13   Prob > F        =    0.0000
    Residual |  7.9365e+10       550   144299703   R-squared       =    0.9995
-------------+----------------------------------   Adj R-squared   =    0.9995
       Total |  1.4841e+14       554  2.6788e+11   Root MSE        =     12012

------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     lotsize |   46.06054   .1690975   272.39   0.000     45.72838    46.39269
        sqft |   357.0088   2.308493   154.65   0.000     352.4743    361.5434
       baths |   227857.4   1679.189   135.69   0.000       224559    231155.8
      hbaths |   176737.2   2815.386    62.78   0.000     171206.9    182267.4
       _cons |  -314441.1   3653.878   -86.06   0.000    -321618.4   -307263.9
------------------------------------------------------------------------------

. eststo
(est2 stored)

. 
. 
. * HC-2a Heteroskedasticity Consistent 2 using Stata's "hat"
. gen var_est = uhat2/(1-my_leverage)
(73 missing values generated)

. 
. reg price lotsize sqft baths hbaths [w = 1/var_est]
(analytic weights assumed)
(sum of wgt is 3.83880641789e-06)

      Source |       SS           df       MS      Number of obs   =       555
-------------+----------------------------------   F(4, 550)       >  99999.00
       Model |  1.4804e+14         4  3.7010e+13   Prob > F        =    0.0000
    Residual |  7.8964e+10       550   143571217   R-squared       =    0.9995
-------------+----------------------------------   Adj R-squared   =    0.9995
       Total |  1.4812e+14       554  2.6736e+11   Root MSE        =     11982

------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     lotsize |   46.06034    .168974   272.59   0.000     45.72842    46.39225
        sqft |   357.0083   2.304784   154.90   0.000      352.481    361.5355
       baths |   227857.7   1676.636   135.90   0.000     224564.3    231151.1
      hbaths |   176728.8   2818.061    62.71   0.000     171193.4    182264.3
       _cons |  -314429.4   3652.734   -86.08   0.000    -321604.4   -307254.4
------------------------------------------------------------------------------

. eststo
(est3 stored)

. 
. 
. *FGLS
. 
. gen lnuhat2 = ln(uhat2)
(73 missing values generated)

. reg lnuhat2 lotsize sqft baths hbaths

      Source |       SS           df       MS      Number of obs   =       555
-------------+----------------------------------   F(4, 550)       =     36.93
       Model |  761.278668         4  190.319667   Prob > F        =    0.0000
    Residual |  2834.80828       550  5.15419687   R-squared       =    0.2117
-------------+----------------------------------   Adj R-squared   =    0.2060
       Total |  3596.08695       554  6.49113168   Root MSE        =    2.2703

------------------------------------------------------------------------------
     lnuhat2 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     lotsize |   .0000508   .0000181     2.80   0.005     .0000152    .0000863
        sqft |   .0003567   .0001669     2.14   0.033     .0000289    .0006845
       baths |   .2528932   .1061162     2.38   0.018     .0444506    .4613358
      hbaths |   .1373787   .1885066     0.73   0.466    -.2329024    .5076597
       _cons |   22.12785   .2449073    90.35   0.000     21.64679    22.60892
------------------------------------------------------------------------------

. predict yhatFGLS
(option xb assumed; fitted values)
(73 missing values generated)

. 
. gen varEst = exp(yhatFGLS)
(73 missing values generated)

. 
. gen wgt_varEst = 1/varEst
(73 missing values generated)

. sum wgt_varEst

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
  wgt_varEst |        555    2.96e-11    2.35e-11   1.20e-13   1.25e-10

. replace wgt_varEst = wgt_varEst/r(sum)
(555 real changes made)

. sum wgt_varEst, detail

                         wgt_varEst
-------------------------------------------------------------
      Percentiles      Smallest
 1%     .0000333       7.34e-06
 5%     .0001119       .0000174
10%     .0002091       .0000259       Obs                 555
25%     .0005619       .0000265       Sum of Wgt.         555

50%     .0014882                      Mean           .0018018
                        Largest       Std. Dev.      .0014305
75%     .0027728       .0054866
90%     .0040163       .0061208       Variance       2.05e-06
95%     .0045008       .0063084       Skewness       .7997119
99%     .0053143       .0076061       Kurtosis       2.955046

. 
. sort wgt_varEst

. 
. gen newvar1 = sum(wgt_varEst)

. gen nobs1 = _n

. scatter newvar1 nobs1 if wgt_varEst != .

. 
. graph export "ex04 A wgt_varEst.png", replace
(file ex04 A wgt_varEst.png written in PNG format)

. 
. reg price lotsize sqft baths hbaths [w = wgt_varEst]
(analytic weights assumed)
(sum of wgt is 1)

      Source |       SS           df       MS      Number of obs   =       555
-------------+----------------------------------   F(4, 550)       =    347.29
       Model |  1.6709e+14         4  4.1772e+13   Prob > F        =    0.0000
    Residual |  6.6153e+13       550  1.2028e+11   R-squared       =    0.7164
-------------+----------------------------------   Adj R-squared   =    0.7143
       Total |  2.3324e+14       554  4.2101e+11   Root MSE        =    3.5e+05

------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     lotsize |   25.01053   4.375847     5.72   0.000     16.41511    33.60595
        sqft |   381.3096   32.90211    11.59   0.000     316.6804    445.9388
       baths |   184921.2   20841.78     8.87   0.000       143982    225860.4
      hbaths |   132213.6   30293.85     4.36   0.000     72707.79    191719.4
       _cons |   977.6043   44144.98     0.02   0.982    -85735.79       87691
------------------------------------------------------------------------------

. eststo
(est4 stored)

. esttab

----------------------------------------------------------------------------
                      (1)             (2)             (3)             (4)   
                    price           price           price           price   
----------------------------------------------------------------------------
lotsize             46.02***        46.06***        46.06***        25.01***
                   (8.65)        (272.39)        (272.59)          (5.72)   

sqft                356.4***        357.0***        357.0***        381.3***
                   (7.27)        (154.65)        (154.90)         (11.59)   

baths            228265.4***     227857.4***     227857.7***     184921.2***
                   (7.32)        (135.69)        (135.90)          (8.87)   

hbaths           180950.8**      176737.2***     176728.8***     132213.6***
                   (3.27)         (62.78)         (62.71)          (4.36)   

_cons           -318356.1***    -314441.1***    -314429.4***        977.6   
                  (-4.42)        (-86.06)        (-86.08)          (0.02)   
----------------------------------------------------------------------------
N                     555             555             555             555   
----------------------------------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001

. 
. log close
      name:  <unnamed>
       log:  wk04 FGLS Answers.log
  log type:  text
 closed on:  15 Feb 2026, 06:28:30
---------------------------------------------------------------------------------------------------------------
